#include "widget.h"
#include "./ui_widget.h"
#include "register.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QMessageBox>
#include "cmd.h"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->com=new communicate;
    connect(com,SIGNAL(m_rstLogin(QJsonObject)),this,SLOT(m_rstLogin(QJsonObject)));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    QString username=ui->lineEdit->text();
    QString password=ui->lineEdit_2->text();
    //将登录信息发送到服务器
    QJsonObject json;
    json["cmd"]=LOGIN;
    json["userName"]=username;
    json["passwd"]=password;
    com->writeClientData(json);
}


void MainWindow::on_pushButton_2_clicked()
{
    this->hide();
    Register reg(com);
    reg.exec();
    this->show();
}

void MainWindow::m_rstLogin(QJsonObject json)
{
    int result=json["result"].toInt();
    qDebug()<<result<<"\n";
    switch (result) {
    case OK:{
        this->hide();
        BrainStorm *b=new BrainStorm(this->com,json);
        b->show();
        b->setAttribute(Qt::WA_DeleteOnClose);
        break;}
    case ERROR:
        QMessageBox::information(this,"错误","未知错误");
        break;
    case USERLOGIN:
        QMessageBox::information(this,"登录","用户已登录");
        break;
    case NAMEORPASSWD:
        QMessageBox::information(this,"登录","密码或用户名错误");
        break;

    default:
        break;
    }
}



